package com.micromall.common.utils;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.io.IOException;
import java.io.StringWriter;

/**
 * Created by DanteFung on 2017/6/21.
 */
public class XMLUtil {

    private static final XmlMapper xml = new XmlMapper();


    public static String write2XML(Object object){
        StringWriter sw = new StringWriter();
        try {
            xml.writeValue(sw, object);
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return sw.toString();
    }

    public static <T> T XML2Object(String xmlStr, Class<T> clazz){
        T t = null;
        try {
            t = xml.readValue(xmlStr, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            new RuntimeException(e);
        }
        return t;
    }
}
